package com.maze.game.view
{
	import com.maze.app.AppText;
	import com.maze.game.board.Board;
	import com.maze.game.control.CoolButton;
	import com.maze.game.event.GameEvent;
	
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.BevelFilter;
	import flash.filters.DropShadowFilter;
	import flash.geom.Matrix;
	
	public class GameTestView extends Sprite
	{
		private var panel:Sprite;
		private var board:Board;
		private var quitBtn:CoolButton;
		private var solveBtn:CoolButton;
		private var viewWidth:uint;
		private var viewHeight:uint;
		
		public function GameTestView()
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			viewWidth = stage.stageWidth;
			viewHeight = stage.stageHeight;
			stage.addEventListener(Event.RESIZE, initMain);
			stage.dispatchEvent(new Event(Event.RESIZE));
		}
		
		private function initMain(evt:Event = null):void
		{
			if(stage.stageWidth>0)
				stage.removeEventListener(Event.RESIZE, initMain);
			else return;
			createContent();
		}
		
		private function quitGame():void
		{
			dispatchEvent(new GameEvent(GameEvent.QUIT_GAME));
		}
		
		private function createContent():void
		{
			panel = getBasePanel();
			addChild(panel);
			panel.x = stage.stageWidth/2 - panel.width/2;
			panel.y = stage.stageHeight/2 - panel.height/2;
			
			var btnWidth:uint = 110;
			var btnHeight:uint = 50;
			
			var paddingH:uint = 25;
			var posx:uint = viewWidth - btnWidth - paddingH;
			var posy:uint = panel.y + paddingH;
			
			solveBtn = new CoolButton(AppText.SHOW_SOLUTION, btnWidth, btnHeight);
			solveBtn.addEventListener(CoolButton.EVENT_CLICK, solveMaze, false, 0, true);
			panel.addChild(solveBtn);
			solveBtn.x = posx;
			solveBtn.y = posy; posy += btnHeight+paddingH;
			
			quitBtn = new CoolButton(AppText.QUIT, btnWidth, btnHeight);
			var quitClick:Function;
			quitBtn.addEventListener(CoolButton.EVENT_CLICK, quitClick = function(evt:Event):void{
				quitBtn.removeEventListener(CoolButton.EVENT_CLICK, quitClick);
				quitGame();
			});
			panel.addChild(quitBtn);
			quitBtn.x = posx;
			quitBtn.y = posy; posy += btnHeight+paddingH;
			
			createMaze();
		}
		
		private function getBasePanel():Sprite
		{
			var panelWidth:uint = viewWidth;
			var panelHeight:uint = viewHeight;
			var base:Sprite = new Sprite();
			var colors:Array = [ 0xffffff, 0xC2C6CA, 0xa9b8c3];
			var alphas:Array = [ 1, 1, 1 ];
			var ratios:Array = [ 0x00, 0xB5, 0xFF ];
			var matrix:Matrix = new Matrix();
			var rounded:uint = 0;
			matrix.createGradientBox(panelWidth, panelHeight, (Math.PI / 2), 0, 0);
			base.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD);
			base.graphics.drawRoundRect(0, 0, panelWidth, panelHeight, rounded, rounded);
			base.graphics.endFill();
			var filterB:BevelFilter = new BevelFilter(1, 60, 0xffffff, 1, 0x5c5c5c, .5, 1.1, 2.5, 4, 3, "inner", false);
			var filterD:DropShadowFilter = new DropShadowFilter(0, 60, 0x000000, .5, 60, 20, 1, 2, false, false, false);
			base.filters = [filterD,filterB];
			return base;
		}
		
		private function createMaze():void
		{
			board = new Board();
			board.editable = true;
			panel.addChild(board);
			board.x = panel.width/2 - board.boardWidth/2;
			board.y = panel.height/2 - board.boardHeight/2;
		}
		
		private function solveMaze(evt:Event = null):void
		{
			board.drawSolution();
		}
	}
}